<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if(!class_exists('CI_Model')) { class CI_Model extends Model {} }

class Pg_model extends CI_Model {
	var $perpage = 10;
	var $url;
	public function __construct() {
	   parent::__construct();
	   $this->url = $this->uri->segment(1);
	}

	function index($args) {
		$data['url'] = $this->url;
		$id = $args[0];
		if ($id == "") $id = 1;
		$data['from'] = $from = $this->input->get('from', true);
		if ($from == "") $from = 0;
		$data['pages'] = $pages = $this->db->select()->from('pg')->where('pg_status', 1)->order_by('pg_date', 'desc')->where('pg_parent', $id)->limit($this->perpage, $from)->get()->result_array();

		/* pagination */
		$count = $this->db->select()->from('pg')->where('pg_status', 1)->where('pg_parent', $id)->count_all_results();
		if ($from != 0 && sizeof($pages) <= 0 && $count > 0) {
			$pgng = $from - $this->perpage;
			redirect($this->url.'/?from='.$pgng);
		}
		$this->load->library('pagination');
		$config['base_url'] = '/'.$this->url.'/';
		$config['total_rows'] = $count;
		$config['page_query_string'] = TRUE;
		$config['per_page'] = $this->perpage;
		$this->pagination->initialize($config);
		$data['paging'] = $this->pagination->create_links();
		/* pagination */
		$out['text'] = $this->load->view('modules/pg/index', $data, true);
		return $out;
	}
	function show($args) {
		$data['url'] = $this->url;
		$id = $args[0];
		$data['page'] = $page = $this->db->select()->from('pg')->where('pg_id', $id)->get()->row_array();
		$data['ph'] = $this->db->select()->from('ph')->where('ph_pg', $id)->order_by('ph_id', 'desc')->get()->result_array();
		if (sizeof($page) <= 0 || $page['pg_status'] == 0) redirect('/');
		$out['data']['crumps'] = " &raquo; <a href='/".$this->url."/".$page['pg_id']."/'>".$page['pg_title']."</a>";
		$out['data']['title'] = $page['pg_title'];
		$out['text'] = $this->load->view('modules/pg/show', $data, true);
		return $out;
	}
	function video() {
		$args[0] = 3;
		return $this->index($args);
	}
}